<template>
	<view>
		<view class="ulist">
			<loadstatusVue :status="data.loadStatus">
				<view class="item" v-for="(item, index) in data.list" :key="index" @click="goActivity(item)">
					<view class="right_item_block">
						<view class="t">
							<image :src="item.imgFace" mode="aspectFill" :lazy-load="true"></image>
						</view>
						<view class="right_cont">
							<view class="">
								<view class="flex_jc_sb">
									<view class="title">{{ item.name }}</view>
									<view class="" @click.stop="handleRemove(item)">
										<uni-icons type="trash" size="20" color="#b19561"></uni-icons>
									</view>
								</view>
								<view class="tip flex">
									<view class="t1">{{ item.type }}</view>
									<view class="t2" v-if="item.sign == '1'">签到</view>
									<view class="t2">{{ item.lineType == 1 ? '线上' : '线下' }}</view>
								</view>
							</view>
							<view class="">
								<view class="tips flex" v-if="item.beginTime">
									<image src="/static/a1.png" mode=""></image>
									<view class="">{{ $formatDateTime(item.beginTime) }}</view>
								</view>
								<view style="margin-top: 6rpx" class="tips flex" v-if="item.lineType == 1">
									<image src="/static/a3.png" mode=""></image>
									<view class="">{{ item.platform }}</view>
								</view>
								<view style="margin-top: 6rpx" class="tips flex" v-else>
									<image src="/static/a2.png" mode=""></image>
									<view class="ts">{{ item.addressInfo }}</view>
								</view>
							</view>
						</view>
					</view>
				</view>
			</loadstatusVue>
		</view>
	</view>
</template>

<script setup>
import { ref, reactive, getCurrentInstance } from 'vue';
import { onLoad, onShow, onReachBottom } from '@dcloudio/uni-app';
import { getDraftList, removeActivity } from '@/api/activity.js';
import loadstatusVue from '@/components/loadstatus/loadstatus.vue';
import { toast, showConfirm } from '@/utils/common.js';
// 所有数据都定义在 data 中
const data = reactive({
	list: [],
	loadStatus: 'before', // 加载状态
	hasMore: true, // 是否有更多数据
	isLoading: false, // 防止重复请求的锁
	queryParams: {
		pageNum: 1,
		pageSize: 10
	}
});
// 延迟函数
const delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
// 设置加载状态的方法
const setLoadStatus = (status, options = {}) => {
	data.loadStatus = status;
};
// 处理响应数据的通用方法
const handleResponseData = async (res, isLoadMore = false) => {
	// 如果是加载更多，确保loading状态至少显示500ms，避免闪烁
	if (isLoadMore) {
		await delay(500);
	}
	if (res.code == 200) {
		// 处理数据
		if (isLoadMore) {
			data.list.push(...res.rows);
		} else {
			data.list = res.rows || [];
		}
		// 判断是否有更多数据
		data.hasMore = res.rows && res.rows.length >= data.queryParams.pageSize;
		// 设置状态
		if (data.list.length === 0) {
			setLoadStatus('empty');
		} else if (!data.hasMore) {
			setLoadStatus('noMore');
		} else {
			setLoadStatus('success');
		}
		return true;
	}
	return false;
};

// 获取列表数据
const getList = async (isLoadMore = false) => {
	// 防止重复请求
	if (data.isLoading) return;
	try {
		data.isLoading = true;
		if (isLoadMore) {
			if (!data.hasMore) return;
			setLoadStatus('loading');
		} else {
			setLoadStatus('before');
			// 初始加载时显示loading至少1秒，避免闪烁
			await delay(500);
		}
		const res = await getDraftList(data.queryParams);
		const success = await handleResponseData(res, isLoadMore);
		if (!success) {
			// 请求失败的处理
			setLoadStatus(data.list.length === 0 ? 'empty' : 'success');
		}
	} catch (err) {
		await delay(500);
		setLoadStatus(data.list.length === 0 ? 'empty' : 'success');
	} finally {
		data.isLoading = false;
	}
};

const handleRemove = (item) => {
	showConfirm('是否要删除当前草稿?').then((res) => {
		if (res.confirm) {
			removeActivity({ id: item.id }).then((res) => {
				console.log(res);
				if (res.code == 200) {
					toast('已删除');
					data.queryParams.pageNum = 1; // 重置页码
					getList();
				}
			});
		}
	});
};
// 跳转活动发布
const goActivity = (item) => {
	uni.navigateTo({
		url:`/mypage/activity/activity?sendType=set&&id=${item.id}`
	});
};
onLoad(() => {
	getList();
});
// 上拉加载更多
onReachBottom(() => {
	if (data.hasMore && (data.loadStatus === 'success' || data.loadStatus === 'noMore') && !data.isLoading) {
		data.queryParams.pageNum++;
		getList(true);
	}
});
</script>

<style lang="scss">
page {
	background: #f7f8f6;
}

.ulist {
	padding: 20rpx;
	.item {
		width: 710rpx;
		height: 230rpx;
		background: #ffffff;
		border-radius: 20rpx;
		padding: 20rpx;
		box-sizing: border-box;
		margin: 0rpx auto 20rpx;
		.right_item_block {
			display: flex;
			width: 100%;
			.t {
				image {
					display: block;
					width: 140rpx;
					height: 190rpx;
					border-radius: 8rpx;
				}
			}
			.right_cont {
				flex: 1;
				margin-left: 12rpx;
				display: flex;
				flex-direction: column;
				box-sizing: border-box;
				justify-content: space-between;
				width: 0;
				.title {
					font-family: PingFang SC, PingFang SC;
					font-weight: 500;
					font-size: 28rpx;
					color: #000000;
					white-space: nowrap;
					overflow: hidden;
					text-overflow: ellipsis;
				}
				.tip {
					margin: 10rpx 0rpx 0rpx 0rpx;
					.t1 {
						padding: 0rpx 6rpx;
						height: 32rpx;
						background: #b19561;
						border-radius: 8rpx 8rpx 8rpx 8rpx;
						font-family: PingFang SC, PingFang SC;
						font-weight: 500;
						font-size: 20rpx;
						color: #ffffff;
						line-height: 32rpx;
						margin-right: 10rpx;
					}
					.t2 {
						padding: 0rpx 6rpx;
						height: 28rpx;
						border-radius: 8rpx 8rpx 8rpx 8rpx;
						font-family: PingFang SC, PingFang SC;
						font-weight: 500;
						font-size: 20rpx;
						color: #615b56;
						line-height: 28rpx;
						border: 1px solid #615b56;
						margin-right: 10rpx;
					}
				}
				.tips {
					image {
						width: 24rpx;
						height: 24rpx;
						margin-right: 10rpx;
						border-radius: 12rpx;
					}
					font-family: PingFang SC, PingFang SC;
					font-weight: 500;
					font-size: 20rpx;
					color: #8f8f8b;
					.ts {
						flex: 1;
						white-space: nowrap;
						overflow: hidden;
						text-overflow: ellipsis;
					}
				}
			}
		}
	}
}
</style>
